/*
    Copyright 2013 Jonathan Kaschak

    This file is part of TerraTamper.

    TerraTamper is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    TerraTamper is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with TerraTamper.  If not, see <http://www.gnu.org/licenses/>.
*/

package Main;

public class TerrariaProtocol {
    
    public static int message_length = 0;
    static int message_type = 4;

    public static int byte2int32(byte byte0, byte byte1, byte byte2, byte byte3) {
        return ((byte3 & 0xFF) << 24) | 
               ((byte2 & 0xFF) << 16) | 
               ((byte1 & 0xFF) << 8)  |
                (byte0 & 0xFF);
    }
    public static byte[] int322byte(int value){
        byte[] bytes = new byte[4];
        bytes[3] = (byte) ((value >> 24) & 0xFF);
        bytes[2] = (byte) ((value >> 16) & 0xFF);
        bytes[1] = (byte) ((value >> 8) & 0xFF);
        bytes[0] = (byte) (value & 0xFF);
        return bytes;
    } public static int byte2int16(byte byte0, byte byte1) {
        return ((byte1 & 0xFF) << 8)  |
                (byte0 & 0xFF);
    }
    public static byte[] int162byte(int value){
        byte[] bytes = new byte[2];
        bytes[1] = (byte) ((value >> 8) & 0xFF);
        bytes[0] = (byte) (value & 0xFF);
        return bytes;
    }
    public static String getEndString(byte[] data, int start){
        String value = "";
        for (int i = 0; i < getMessageLength(data)-start; i++) {
            value += (char)data[start+i];
        }
        return value;
    }
    public static byte[] setEndString(byte[] data, int start, String in){
        char[] arr = in.toCharArray();
        for (int i = 0; i < in.length(); i++) {
            data[i+start] = (byte)arr[i];
        }
        return data;
    }
    public static byte[] reconstruct(byte[] data, byte[] newdata) {
        System.arraycopy(newdata, 0, data, 0, newdata.length);
        return data;
    }
    public static byte[] dataadd(byte[] data, int numberRead, byte[] newdata, int newNumberRead) {
        System.arraycopy(newdata, 0, data, numberRead, newNumberRead);
        return data;
    }
    static int getMessageType(byte[] data) {
        return data[message_type];
    }
    static int getMessageLength(byte[] data) {
        return byte2int32(data[message_length],
                          data[message_length+1],
                          data[message_length+2],
                          data[message_length+3]) + 4;
    }
}
